package com.example.juc.singe;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

//枚举类单例模式
public enum EnumTest {
    INSTANCE;
    public EnumTest Test(){
        return INSTANCE;
    }
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Constructor<EnumTest> declaredConstructor = EnumTest.class.getDeclaredConstructor(String.class, int.class);
        declaredConstructor.setAccessible(true);
        //用反射不能破坏枚举类
        //Exception in thread "main" java.lang.IllegalArgumentException: Cannot reflectively create enum objects
        //	at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
        EnumTest e1=declaredConstructor.newInstance();
        EnumTest e2=EnumTest.INSTANCE;
        System.out.println(e1==e2);
    }
}
